package com.ITOT;

import java.text.SimpleDateFormat;
import java.util.Date;
import gameEngine.Score;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidhive.dashboard.R;

@SuppressLint({ "NewApi", "Override" })
public class AnswerFragment extends DialogFragment implements
		View.OnClickListener {
	private Button okBtn, cancelBtn, backBtn;
	private EditText answerText;
	private int rightAnswer;
	private TextView resultText;
	private LinearLayout answerLayout, resultLayout;
	private TextView compAnswerText;
	private String mode, card, name;
	private int time;

	public AnswerFragment() {
		// Empty constructor required for DialogFragment
	}

	public void setRightAnswer(int rightAnswer) {
		this.rightAnswer = rightAnswer;
	}

	boolean isComp;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_answer, container);
		isComp = getArguments().getBoolean("comp");
		rightAnswer = getArguments().getInt("secretNum");
		mode = getArguments().getString("mode");
		card = getArguments().getString("card");
		name = getArguments().getString("name");
		time = getArguments().getInt("time");
		answerLayout = (LinearLayout) view.findViewById(R.id.answering);
		answerLayout.setVisibility(View.VISIBLE);
		resultLayout = (LinearLayout) view.findViewById(R.id.result);
		resultLayout.setVisibility(View.INVISIBLE);
		resultText = (TextView) view.findViewById(R.id.resultText);
		okBtn = (Button) view.findViewById(R.id.ok);
		okBtn.setOnClickListener(this);
		cancelBtn = (Button) view.findViewById(R.id.cancel);
		cancelBtn.setOnClickListener(this);
		backBtn = (Button) view.findViewById(R.id.back);
		backBtn.setOnClickListener(this);
		answerText = (EditText) view.findViewById(R.id.answerText);
		compAnswerText = (TextView) view.findViewById(R.id.compAnswerText);
		if (isComp) {
			answerText.setVisibility(View.INVISIBLE);
			compAnswerText.setVisibility(View.VISIBLE);
			compAnswerText.setText("Your number is " + rightAnswer);
		} else {
			answerText.setVisibility(View.VISIBLE);
			compAnswerText.setVisibility(View.INVISIBLE);
		}
		getDialog().setTitle("Answer");

		return view;
	}

	private DatabaseReadWrite dba;

	@Override
	public void onClick(View v) {
		if (v == okBtn) {
			if (!isComp) {
				if (Integer.parseInt(answerText.getText().toString().trim()) == rightAnswer) {
					Score s = new Score();
					s.setCard(card);
					s.setName(name);
					s.setMode(mode);
					s.setTime(time);
					SimpleDateFormat sdf = new SimpleDateFormat(
							"dd/MM/yyyy hh.mm.ss");
					String format = sdf.format(new Date());
					s.setTimestamp(format);
					dba = new DatabaseReadWrite(this.getActivity());
					dba.open();
					dba.insertScore(s);
					dba.close();
					answerLayout.setVisibility(View.INVISIBLE);
					resultLayout.setVisibility(View.VISIBLE);
					resultText.setText("Your answer is correct!");
					this.getDialog().setTitle("Result");
				} else {
					this.getDialog().setTitle("Result");
					answerLayout.setVisibility(View.INVISIBLE);
					resultLayout.setVisibility(View.VISIBLE);
					resultText
							.setText("Sorry, Your answer is wrong.\nThe correct number is "
									+ rightAnswer);
				}
				InputMethodManager imm = (InputMethodManager) getActivity()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
			}else {
				this.dismiss();
				this.getActivity().finish();
			}
		} else if (v == cancelBtn) {
			this.dismiss();
		} else if (v == backBtn) {
			this.dismiss();
			this.getActivity().finish();
		}

	}
}
